//
//  VisitorView.swift
//  微博
//
//  Created by 段瑞金 on 16/5/11.
//  Copyright © 2016年 段瑞金. All rights reserved.
//

import UIKit
import SnapKit

class VisitorView: UIView {


    //MARK:-生命周期方法

    override init(frame: CGRect) {
        super.init(frame: frame)
//        backgroundColor = UIColor.redColor()
        setUpSubViews()
        startAnimate()
    }
    // 只要实现了通过代码创建的构造方法, 就必须实现通过文件创建的构造方法
    // 注意: 默认情况下, 通过文件创建的构造方法, 会调用致命错误方法, 该方法会导致程序直接崩溃
    // Swift这样做的目的是为了让我们更简单高效开发
    // Swift推荐要么通过纯代码, 要么通过文件
    // 当前我们也可以即实现纯代码, 又实现文件

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        setUpSubViews()
    }
    
    //MARK:-给外界提供一个方法,在不同的控制器调用这个方法的时候,界面显示不同的画面,和提示内容
    func setUpVisitorViewInfo(imageName : String? , tipText : String?) -> () {
        
        if imageName == nil {
            startAnimate()
            return
        }
        //设置文本
        tipLabel.text = tipText
        //设置图标
        iconImageView.image = UIImage(named: imageName ?? "")
        //隐藏动画
        imageView.hidden = true
    }

    
    //MARK:-懒加载
    //转盘
    private lazy var imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))

    //蒙版
    private lazy var coverImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //icon
    private lazy var iconImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //提示框
    private lazy var tipLabel : UILabel = {
       let tipLabel = UILabel()
        tipLabel.text = "关注一些人, 回这里看看有没有什么惊喜"
        tipLabel.numberOfLines = 0
        tipLabel.textColor = UIColor.lightGrayColor()
        tipLabel.textAlignment = NSTextAlignment.Center
        return tipLabel
    }()
    //注册按钮
    lazy var leftRegisterBtn : UIButton = {
       let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        return btn
    }()
    //登录按钮
   lazy var rightLoginBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: .Normal)
        return btn
    }()

}



//MARK:-布局添加控件

extension VisitorView {
    //动画
    private func startAnimate () -> (){
        let animate = CABasicAnimation(keyPath: "transform.rotation")
        animate.toValue = M_PI * 2
        animate.repeatCount = MAXFLOAT
        animate.duration = 5.0
        animate.removedOnCompletion = false
        imageView.layer.addAnimation(animate, forKey: nil)
        
    }
    
    private func setUpSubViews () -> (){
        //添加控件
        addSubview(imageView)
        addSubview(coverImageView)
        addSubview(iconImageView)
        addSubview(tipLabel)
        addSubview(leftRegisterBtn)
        addSubview(rightLoginBtn)

        //布局控件
        //转盘
        imageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
            
        }
        //蒙版
        coverImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
            
        }
        //icon
        iconImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(16)
        }
        //提示框
        tipLabel.snp_makeConstraints { (make) in

            make.top.equalTo(imageView.snp_bottom).offset(15)
            make.centerX.equalTo(self)
            //            make.left.equalTo(imageView).offset(-10)
            //            make.right.equalTo(imageView).offset(10)
            make.width.equalTo(226)

            
            
        }
        //左侧按钮
        leftRegisterBtn.snp_makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp_bottom).offset(15)
            make.left.equalTo(tipLabel)
            make.height.equalTo(30)
            make.width.equalTo(100)
        }
        
        //右侧按钮
        rightLoginBtn.snp_makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp_bottom).offset(15)
            make.right.equalTo(tipLabel)
            make.height.equalTo(30)
            make.width.equalTo(100)
        }
        
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
    }
}














